#  encoding=utf-8
#! /usr/sbin
from excelword.config import host
import requests
import json
import sys
#加上避免python警告看着难受
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from excelword.util.matchjson import MatchJsonData
from excelword.util.adapturl import adaptionurl
class operatexls():
    def __init__(self):
        self.url = "None"
    def addresource(self,url,data,header):
        # 判断url 是否为完全路径还是简化路径，先通过分割后第一个字符来判断
        self.url = adaptionurl(url)
        try:
        #下面部分不需要改动
            data = MatchJsonData().get_data(data)
            json_post_data=json.dumps(data,ensure_ascii=False)
            response_data = requests.post(self.url,headers=header,data=json_post_data,verify=False)
            return json.dumps(json.loads(response_data.text)["data"])
        except Exception:
            return  '{"code":124,"message":"API操作异常"}'
            # message = json.loads(response_data.text)["message"]
            # scode = response_data.status_code   #状态码
        #     if message=="成功":
        #         print("新增资源成功")
        #         return '{"code":0,"message":"操作成功"}'
        #     else:
        #         print("新增资源失败，失败信息为:{message}!".format(message=message))
        #         return '{"code":1,"message":"操作失败"}'
        # except Exception as e:
        #     print("API操作产生异常，异常信息为:{error_message}".format(error_message=e))
        #     return '{"code":124,"message":"API操作异常"}'
        #     sys.exit(0)

    def getresource(self,url,data,header):
        self.url = adaptionurl(url)
        try:
            # 下面部分不需要改动
            response_data = requests.get(self.url, headers=header, verify=False)
            scode = response_data.status_code   #状态码
            # return json.loads(response_data.text)
            return json.dumps(json.loads(response_data.text)["data"])
        except Exception:
            return  '{"code":124,"message":"API操作异常"}'
        #     message = json.loads(response_data.text)["message"]
        #     if message == "成功":
        #         print("查看资源成功")
        #         return '{"code":0,"message":"操作成功"}'
        #     else:
        #         print("查看资源失败，失败信息为:{message}!".format(message=message))
        #         return '{"code":1,"message":"操作失败"}'
        # except Exception as e:
        #     print("API操作产生异常，异常信息为:{error_message}".format(error_message=e))
        #     return '{"code":124,"message":"API操作异常"}'
        #     sys.exit(0)

    def patchresource(self,url,data,header):
        # 判断url 是否为完全路径还是简化路径，先通过分割后第一个字符来判断
        self.url = adaptionurl(url)
        try:
        #下面部分不需要改动
            data = MatchJsonData().get_data(data)
            json_post_data=json.dumps(data,ensure_ascii=False)
            response_data = requests.patch(self.url,headers=header,data=json_post_data,verify=False)
            scode = response_data.status_code  # 状态码
            return json.dumps(json.loads(response_data.text)["data"])
        except Exception:
            return  '{"code":124,"message":"API操作异常"}'
        #     message = json.loads(response_data.text)["message"]
        #     if message=="成功":
        #         print("修改资源成功")
        #         return '{"code":0,"message":"操作成功"}'
        #     else:
        #         print("修改资源失败，失败信息为:{message}!".format(message=message))
        #         return '{"code":1,"message":"操作失败"}'
        # except Exception as e:
        #     print("API操作产生异常，异常信息为:{error_message}".format(error_message=e))
        #     return '{"code":124,"message":"API操作异常"}'
        #     sys.exit(0)

    def deleteresource(self,url,data,header):
        # 判断url 是否为完全路径还是简化路径，先通过分割后第一个字符来判断
        self.url = adaptionurl(url)
        try:
        #下面部分不需要改动
            response_data = requests.delete(self.url,headers=header,data=None,verify=False)
            scode = response_data.status_code  # 状态码
            # return json.loads(response_data.text)
            return json.dumps(json.loads(response_data.text)["data"])
        except Exception:
            return  '{"code":124,"message":"API操作异常"}'
        #     message = json.loads(response_data.text)["message"]
        #     if message=="成功":
        #         print("删除资源成功")
        #         return '{"code":0,"message":"操作成功"}'
        #     else:
        #         print("删除资源失败，失败信息为:{message}!".format(message=message))
        #         return '{"code":1,"message":"操作失败"}'
        # except Exception as e:
        #     print("API操作产生异常，异常信息为:{error_message}".format(error_message=e))
        #     return '{"code":124,"message":"API操作异常"}'
        #     sys.exit(0)
    def operation(self,url,methods,data,header = None):
        res = None
        #使用字典来映射方法，提供入口，具体逻辑通过methods 映射到不同函数上
        methodsdict = {"post":self.addresource,"get":self.getresource,"patch":self.patchresource,"delete":self.deleteresource}
        #传入的header 判断
        if(header == None):
            res = '{"code":122,"message":"对不起，必须传入header"}'
            return res
        elif("Cookie" not in header):
            res = '{"code":123,"message":"对不起，为携带token 信息，请进行登陆"}'
            return res
        #通过methodsdict 来映射方法
        else:
            res = methodsdict[methods](url,data,header)
            return res
